<?php
/**
 * @version		$Id: content.php 11371 2008-12-30 01:31:50Z ian $
 * @package		Joomla
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$mainframe->registerEvent( 'onSearch', 'plgSearchJajob' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchJajobAreas' );

//JPlugin::loadLanguage( 'plg_search_content' );

/**
 * @return array An array of search areas
 */
function &plgSearchJajobAreas()
{
	static $areas = array(
		'jajob' => 'JaJobboard'
	);
	return $areas;
}

/**
 * Content Search method
 * The sql must return the following fields that are used in a common display
 * routine: href, title, section, created, text, browsernav
 * @param string Target search string
 * @param string mathcing option, exact|any|all
 * @param string ordering option, newest|oldest|popular|alpha|category
 * @param mixed An array if the search it to be restricted to areas, null if search all
 */
function plgSearchJajob( $text, $phrase='', $ordering='', $areas=null )
{
	global $mainframe;

	$db		=& JFactory::getDBO();
	$user	=& JFactory::getUser();

	require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'router.php');
	require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'helper'.DS.'jafunctions.class.php');
	component_configs();
	require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'models'.DS.'jajobs.php');
	require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_search'.DS.'helpers'.DS.'search.php');
	$modelJob = new JobBoardModelJAJobs();

	$searchText = $text;
	if (is_array( $areas )) {
		if (!array_intersect( $areas, array_keys( plgSearchJajobAreas() ) )) {
			return array();
		}
	}

	// load plugin params info
 	$plugin			=& JPluginHelper::getPlugin('search', 'jajob');
 	$pluginParams	= new JParameter( $plugin->params );

	$sStandard = $pluginParams->get( 'search_standard', 	1 );
	$sPremium 		= $pluginParams->get( 'search_premium', 		1 );
	$limit 			= $pluginParams->def( 'search_limit', 		50 );

	$nullDate 		= $db->getNullDate();
	$date =& JFactory::getDate();
	$now = $date->toMySQL();

	$text = trim( $text );
	if ($text == '') {
		return array();
	}

	$fieldList = getEditFieldList('ja_jobs');
	$wheres = array();
	switch ($phrase) {
		case 'exact':
			$text		= $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
			$wheres2 	= array();
			for ($i=0;$i<count($fieldList);$i++)
				if (in_array($fieldList[$i]->field_type,array('EDITOR','TEXTFIELD','TEXTAREA')))
				{
					$wheres2[] 	= 'a.'.$fieldList[$i]->field_name.' LIKE '.$text;
				}
			$where 		= '(' . implode( ') OR (', $wheres2 ) . ')';
			break;

		case 'all':
		case 'any':
		default:
			$words = explode( ' ', $text );
			$wheres = array();
			foreach ($words as $word) {
				$word		= $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
				$wheres2 	= array();
				for ($i=0;$i<count($fieldList);$i++)
				if (in_array($fieldList[$i]->field_type,array('EDITOR','TEXTFIELD','TEXTAREA')))
				{
					$wheres2[] 	= 'a.'.$fieldList[$i]->field_name.' LIKE '.$word;
				}
				$wheres[] 	= implode( ' OR ', $wheres2 );
			}
			$where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
			break;
	}

	$morder = '';
	switch ($ordering) {
		case 'alpha':
			$order = 'a.title ASC';
			break;

		case 'newest':
			default:
			$order = 'a.effected_date DESC';
			break;
		default:
			$order = 'a.effected_date ASC';
			break;
	}
//var_dump($order);
	$rows = array();

	// search standard job
	if ( $sStandard && $limit > 0 )
	{
		$list1 = $modelJob->getItems(' and ('.$where.') and (a.is_hotjob=0)',$limit,0,$order);
		$limit -= count($list1);

		if(isset($list1))
		{
			foreach($list1 as $key => $item)
			{
				$list1[$key]->href = JURI::root().'index.php?option=com_jajobboard&view=jajobs&layout=jaview&cid[]='.$item->id.'&Itemid='.JRequest::getVar('Itemid');
				$list1[$key]->created = strtotime($list1[$key]->effected_date)?$list1[$key]->effected_date:$list1[$key]->created_date;
				$list1[$key]->browsernav = 1;
				$list1[$key]->section = 0;
				$list1[$key]->text = $list1[$key]->job_detail;
			}
		}

		$rows[] = $list1;
	}

	// search premium job
	if ( $sPremium && $limit > 0 )
	{
		$list2 = $modelJob->getItems(' and ('.$where.') and (a.is_hotjob=1)',$limit,0,$order);
		$limit -= count($list2);

		if(isset($list2))
		{
			foreach($list2 as $key => $item)
			{
				$list1[$key]->href = JURI::root().'index.php?option=com_jajobboard&view=jajobs&layout=jaview&cid[]='.$item->id.'&Itemid='.JRequest::getVar('Itemid');
				$list2[$key]->created = strtotime($list2[$key]->effected_date)?$list2[$key]->effected_date:$list2[$key]->created_date;
				$list2[$key]->browsernav = 1;
				$list2[$key]->section = 0;
				$list2[$key]->text = $list2[$key]->job_detail;
			}
		}

		$rows[] = $list2;
	}


	$results = array();
	if(count($rows)) 
	{
		foreach($rows as $row)
		{
			$new_row = array();
			foreach($row AS $key => $article) {
				if(searchHelper::checkNoHTML($article, $searchText, array('text', 'title', 'metadesc', 'metakey'))) {
					$new_row[] = $article;
				}
			}
			$results = array_merge($results, (array) $new_row);
		}
	}

	return $results;
}
